package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.BitmapTypeRequest;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.AgentCommentVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.businissobject.AgentCommentBo;
import com.centanet.framework.base.BaseAdapter1;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.glide.RoundImageViewTarget;

import java.util.ArrayList;

/**
 * 购房指引
 * Created by guilin on 16/1/22.
 */
public class AgentCommentAdapter extends BaseAdapter1<AgentCommentVH> {

    private final Context context;
    private BitmapTypeRequest<String> mBitmapTypeRequest;
    private final ArrayList<AgentCommentBo> list;
    private RecyclerViewItemOnClickListener<AgentCommentBo> itemOnClickListener;
    private boolean imEnable;
    private String descriptionSplitChar;

    public AgentCommentAdapter(Context context, BitmapTypeRequest<String> mBitmapTypeRequest, ArrayList<AgentCommentBo> list) {
        super(context);
        this.context = context;
        this.mBitmapTypeRequest = mBitmapTypeRequest;
        this.list = list;
    }

    public void setItemOnClickListener(RecyclerViewItemOnClickListener<AgentCommentBo> itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public void setDescriptionSplitChar(boolean imEnable, String descriptionSplitChar) {
        this.imEnable = imEnable;
        this.descriptionSplitChar = descriptionSplitChar;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_agent_comment;
    }

    @Override
    protected AgentCommentVH viewHolder(View view) {
        return new AgentCommentVH(view);
    }

    @Override
    public void onBindViewHolder(AgentCommentVH holder, int position) {
        final AgentCommentBo agentCommentBo = list.get(position);
        if (GlideLoad.needLoad(true)) {
            mBitmapTypeRequest
                    .load(agentCommentBo.getStaffImage())
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(holder.img_staff));
        } else {
            mBitmapTypeRequest
                    .load("")
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(holder.img_staff));
        }
        holder.atv_name.setText(agentCommentBo.getStaffName());
        if (TextUtils.isEmpty(descriptionSplitChar) ||
                !agentCommentBo.getPostDirection().contains(descriptionSplitChar)) {
            holder.atv_content.setText(agentCommentBo.getPostDirection());
        } else {
            holder.atv_content.setText(agentCommentBo.getPostDirection().replaceAll(descriptionSplitChar, "\n"));
        }
        holder.atv_im.setVisibility(imEnable ? View.VISIBLE : View.GONE);
        holder.rl_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick(agentCommentBo, 3);
            }
        });
        holder.atv_im.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick(agentCommentBo, 0);
            }
        });
        holder.atv_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick(agentCommentBo, 1);
            }
        });
        holder.atv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick(agentCommentBo, 2);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private void itemClick(AgentCommentBo agentCommentBo, int tag) {
        if (itemOnClickListener != null) {
            itemOnClickListener.onItemOnClick(agentCommentBo, tag);
        }
    }
}
